#include "rcdata.h"
#include <iostream>

CriticalRegion::CriticalRegion(){
    label=0;
    type=CriticalRegion::NONE;
    forcedtype=CriticalRegion::NONE;

}

void CriticalRegion::setType(RegionType type){
    this->type=type;
}

void CriticalRegion::setLabel(int label){
    this->label=label;
}

void CriticalRegion::forceType(RegionType forcedtype){
    this->forcedtype=forcedtype;
}

void CriticalRegion::set_emergent_vec(unsigned int nvectors){
    this->emergentvectors=nvectors;
}

void CriticalRegion::addPoint(Position position){
    regionpoints.push_back(position);
}

CriticalRegion::RegionType CriticalRegion::getType(){
    return type;
}

int CriticalRegion::getLabel(){
    return label;
}

CriticalRegion::RegionType CriticalRegion::getforcedType(){
    return forcedtype;
}

int CriticalRegion::get_emergent_vec(){
    return emergentvectors;
}

list<Position> CriticalRegion::getPoints(){
    return regionpoints;
}


RCData::RCData(unsigned int imageheight,unsigned int imagewidth){
    nregion=0;
    rclabelling = new Grey_Image(imageheight,imagewidth,65500);
}

RCData::~RCData(){
    delete rclabelling;
}

void RCData::addRegion(CriticalRegion newregion){
    nregion++;
    newregion.setLabel(nregion);
    data.push_back(newregion);


    list<Position> points = newregion.getPoints();

    for(list<Position>::iterator itr=points.begin();itr!=points.end();itr++){
        rclabelling->setPixel(*itr,nregion);
    }

}

Grey_Image* RCData::getImage(){
    return rclabelling;
}

void RCData::forceclassification(Position xy,CriticalRegion::RegionType type){
    int label=rclabelling->getPixel(xy);
    if(label!=0){
        for(list<CriticalRegion>::iterator itr=data.begin();itr!=data.end();itr++){
            if((*itr).getLabel()==label)
                (*itr).forceType(type);
        }

    }
}

CriticalRegion::RegionType RCData::getforcedclassification(Position xy){
    CriticalRegion::RegionType type;
    int label=rclabelling->getPixel(xy);
    if(label!=0){
        for(list<CriticalRegion>::iterator itr=data.begin();itr!=data.end();itr++){
            if((*itr).getLabel()==label)
                type=(*itr).getforcedType();
        }

    }
    else
        type=CriticalRegion::NONE;

    return type;
}

CriticalRegion::RegionType RCData::getforcedclassification(int label){
    CriticalRegion::RegionType type;
    if(label!=0){
        list<CriticalRegion>::iterator itr=data.begin();
        for(int counter=1;counter==label;counter++){
            itr++;
        }
        type=(*itr).getforcedType();
    }
    return type;
}
